package com.softer.chengxin.project.service.impl;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.project.dao.FeeDao;
import com.softer.chengxin.project.domin.ProfitVO;
import com.softer.chengxin.project.service.FeeService;
import com.softer.chengxin.scheduled.dao.ScheduledTaskDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Gong Zhiyang
 * @date 2018/10/17
 */
@Service
public class FeeServiceImpl implements FeeService {
    @Autowired
    private FeeDao feeDao;
    @Autowired
    private ScheduledTaskDao scheduledTaskDao;
    /**
     * 新增经营信息
     *
     * @param profit 经营信息
     * @return 结果
     */
    @Override
    public CommonResult add(ProfitVO profit) {
        if (StringUtils.isBlank(profit.getProjectId())) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请传入项目ID");
        }
        //获取最近设置的定时任务
        String time = scheduledTaskDao.getTime();
        //获取主管部门定义的年份
        String year = DateFormatUtil.getYear(time);
        profit.setYear(year);
        //查询该项目当前年度是否添加
        ProfitVO profitVO = feeDao.detail(profit.getProjectId());
        if (profitVO != null) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "该项目已经添加过");
        }
        int num = feeDao.add(profit);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "新增成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "新增失败");
    }

    /**
     * 查询项目的经营信息
     *
     * @param projectId 项目ID
     * @return 项目信息
     */
    @Override
    public ProfitVO detail(String projectId) {
        return feeDao.detail(projectId);
    }

    /**
     * 编辑项目经营信息
     *
     * @param profit 经营信息
     * @return 结果
     */
    @Override
    public CommonResult edit(ProfitVO profit) {
        int num = feeDao.edit(profit);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "修改成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "修改失败");
    }
}
